package com.wj.dubbo.service.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
@Configuration
@EnableRabbit
public class RabbitMQConfig implements RabbitListenerConfigurer {
	
	public static final String EXCHANGE_NAME = "appExchange";
	public static final String WF_EXCHANGE_NAME = "";
	public static final String QUEUE_CRAWLER_FUND_NAME = "queue.crawler.fund";
	public static final String QUEUE_CRAWLER_SOCIAL_NAME = "queue.crawler.social";
	public static final String QUEUE_WELFARESHOP_BATCH_NAME = "queue.welfareshop.batch"; //福利商城基础信息跑批
	public static final String QUEUE_WELFARESHOP_UPDATE_PRODUCT_NAME = "queue.welfareshop.updateProduct";
    @Bean
	public TopicExchange appExchange() {
		return new TopicExchange(EXCHANGE_NAME);
	}
    
    @Bean
	public TopicExchange wfExchange() {
		return new TopicExchange(WF_EXCHANGE_NAME);
	}

	@Bean
	public Queue queueCrawlerFund() {
		return new Queue(QUEUE_CRAWLER_FUND_NAME);
	}

	@Bean
	public Queue queueCrawlerSocial() {
		return new Queue(QUEUE_CRAWLER_SOCIAL_NAME);
	}
	
	@Bean
	public Queue queueWelfareshopBatch() {
		return new Queue(QUEUE_WELFARESHOP_BATCH_NAME);
	}
	
	@Bean
	public Queue queueWelfareshopUpdateProduct() {
		return new Queue(QUEUE_WELFARESHOP_UPDATE_PRODUCT_NAME);
	}

	@Bean
	public Binding bindingCrawlerFund(TopicExchange appExchange, Queue queueCrawlerFund) {
		return BindingBuilder.bind(queueCrawlerFund).to(appExchange).with(QUEUE_CRAWLER_FUND_NAME);
	}

	@Bean
	public Binding bindingCrawlerSocial(TopicExchange appExchange, Queue queueCrawlerSocial) {
		return BindingBuilder.bind(queueCrawlerSocial).to(appExchange).with(QUEUE_CRAWLER_SOCIAL_NAME);
	}
	
	@Bean
	public Binding bindingWelfareshopBatch(TopicExchange wfExchange, Queue queueWelfareshopBatch) {
		return BindingBuilder.bind(queueWelfareshopBatch).to(wfExchange).with(QUEUE_WELFARESHOP_BATCH_NAME);
	}
	
	@Bean
	public Binding bindingWelfareshopUpdateProduct(TopicExchange wfExchange, Queue queueWelfareshopUpdateProduct) {
		return BindingBuilder.bind(queueWelfareshopUpdateProduct).to(wfExchange).with(QUEUE_WELFARESHOP_UPDATE_PRODUCT_NAME);
	}

	@Bean
	public RabbitTemplate rabbitTemplate(final ConnectionFactory connectionFactory, Jackson2JsonMessageConverter producerJackson2MessageConverter) {
		RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
		rabbitTemplate.setMessageConverter(producerJackson2MessageConverter);
		return rabbitTemplate;
	}

	@Bean
	public Jackson2JsonMessageConverter producerJackson2MessageConverter() {
		return new Jackson2JsonMessageConverter();
	}

	@Bean
	public MappingJackson2MessageConverter consumerJackson2MessageConverter() {
		return new MappingJackson2MessageConverter();
	}

	@Bean
	public DefaultMessageHandlerMethodFactory messageHandlerMethodFactory() {
		DefaultMessageHandlerMethodFactory factory = new DefaultMessageHandlerMethodFactory();
		factory.setMessageConverter(consumerJackson2MessageConverter());
		return factory;
    }

	@Override
	public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) {
		registrar.setMessageHandlerMethodFactory(messageHandlerMethodFactory());
	}

}
